package com.litree.service.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.litree.service.mapper.ICommonMapper;
import com.litree.service.mapper.ILoginMapper;
import com.litree.service.services.ICommonSerivce;
import com.litree.service.vo.LocationVo;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Service
public class CommonSerivce implements ICommonSerivce {

	@Autowired
	ICommonMapper mapper;
	
	@Override
	public List searchAraeInfo(String searchKey,int pageSize,int pageNo) {
		Map param = new HashMap();
		param.put("searchKey", searchKey);
		param.put("pageFrom", (pageNo-1)*pageSize);
		param.put("pageSize", pageSize);
		return mapper.searchAraeInfo(param);
	}

	@Override
	@Transactional
	public void setLoginSearchKey(String loginName, String searchKey) {
		Map param = new HashMap();
		param.put("loginName", loginName);
		param.put("searchKey", searchKey);
		param.put("sortOrder", "ASC");
		List results = mapper.getLoginSearchKeyByLoginName(param);
		if(results.size()<5){
			mapper.setLoginSearchKey(param);			
		}else if(results.size()==5){
			//update first records
			Map row = (Map)results.get(0);
			Long firstSearchKeyId = (Long) row.get("sid");
			param.put("firstSearchKeyId", firstSearchKeyId);
			mapper.updateLoginSearchKey(param);
		}
	}

	@Override
	public List getLastSearchKey(String loginName) {
		Map param = new HashMap();
		param.put("loginName", loginName);
		param.put("sortOrder", "DESC");
		List reslut = mapper.getLoginSearchKeyByLoginNameDistinct(param);
		return reslut;
	}

	@Override
	public List getStationInfo(LocationVo location) {
		return mapper.getStationInfo(location);
	}

	@Override
	public Long getStationInfoCount(LocationVo location) {
		return mapper.getStationInfoCount(location);
	}

	@Override
	public Long searchAraeInfoCount(String searchKey) {
		List list =mapper.searchAraeInfoCount(searchKey);
		long count = 0;
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			count += (long)map.get("count");
		}
		return count;
	}


}
